import { OneModal } from '@/components/one-ui';
import React, { Ref, useRef } from 'react';
import { UpdateNeedMaterialModalRightForm } from './UpdateNeedMaterialModalRightForm';
import { IOneModalRef } from '@/pages/type';
import { onCloseModalCommon } from '@/utils';
import { Button, Form, Spin, message } from 'antd';
import { MaterialService } from '../services/MaterialService';

interface UpdateNeedMaterialAddModalAPI {
  prefixCls: string;
  onClose: () => void;
  onOk: () => void;
}

/**
 * 选择物料弹框中的添加物料弹窗
 * */
export const UpdateNeedMaterialAddModal: React.FC<UpdateNeedMaterialAddModalAPI> = ({ prefixCls, onClose, onOk }) => {
  const addMaterialRef: Ref<IOneModalRef> = useRef(null);

  /**添加物料表单的ref*/
  const [addMaterialForm] = Form.useForm();

  /**回调函数*/
  const addMaterialCallback = (res: Record<string, any>) => {
    if (res?.msgFlag) {
      message?.success(res?.msg);
      onOk();
      onCloseModalCommon(addMaterialRef);
    } else {
      message?.error('物料添加失败，请检查信息！');
    }
  };

  const { addMaterialRun, addMaterialLoading } = MaterialService().addMaterialService(addMaterialCallback);

  return (
    <OneModal
      open={true}
      getContainer={false}
      ref={addMaterialRef}
      title={'添加物料'}
      width={800}
      bodyStyle={{ padding: '15px' }}
      onCancel={onClose}
      footer={[
        <Button
          key="back"
          loading={addMaterialLoading}
          onClick={() => {
            onCloseModalCommon(addMaterialRef);
          }}
          // icon={<CloseCircleOutlined />}
        >
          取消
        </Button>,
        <Button
          key="submit"
          type="primary"
          loading={addMaterialLoading}
          onClick={() => {
            addMaterialForm?.validateFields()?.then((res: any) => {
              addMaterialRun(res);
            });
          }}
          // icon={<CheckCircleOutlined />}
        >
          添加
        </Button>,
      ]}
    >
      <Spin spinning={addMaterialLoading}>
        <UpdateNeedMaterialModalRightForm formRef={addMaterialForm} prefixCls={prefixCls} />
      </Spin>
    </OneModal>
  );
};
